package com.example.project;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.service.ComerParcelable;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Delete_Restaurant_DB extends Activity {

	public static final int MSG_OBTAIN_ALL_RESTAURANTS = 6;
	final  Messenger mMessenger = new Messenger(new IncomingHandler());
	ArrayList<HashMap<String, String>> restaurantList = new ArrayList<HashMap<String, String>>();
	ListView listView; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delete__restaurant__db);
		listView= (ListView) findViewById(R.id.listDelete);
        Bundle b = new Bundle();
		Message msg = Message.obtain(null, DateBaseService.MSG_GET_ALL_RESTAURANTS, 0, 0);
		msg.setData(b);
		msg.replyTo=mMessenger;
		try {
			MainActivity.mService.send(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		/* HashMap<String, String> map = new HashMap<String, String>();
		 map.put("nom", "prova");
		 map.put("rate", "4.6");
		 restaurantList.add(map);
		 DeleteAdapter adapter;
	     adapter=new DeleteAdapter(Delete_Restaurant_DB.this, restaurantList);        
	     listView.setAdapter(adapter);*/
		 
	}
	
	class IncomingHandler extends Handler {
    	@Override
    	public void handleMessage(Message msg) {
    		switch (msg.what) {
    			case Delete_Restaurant_DB.MSG_OBTAIN_ALL_RESTAURANTS: 	
    				restaurantList.clear();
    				Bundle b = msg.getData();
    				b.setClassLoader(ComerParcelable.class.getClassLoader());
    				ComerParcelable cp  =  (ComerParcelable) b.getParcelable("RestaurantParcelable");
    				restaurantList= cp.getRestaurantsList();
    				 DeleteAdapter adapter;
    			     adapter=new DeleteAdapter(Delete_Restaurant_DB.this, restaurantList);        
    			     listView.setAdapter(adapter);
    			     listView.setOnItemClickListener(new OnItemClickListener() {
    		        	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    		        	    	ComerParcelable cp = new ComerParcelable(0,"user", listView.getAdapter().getItem(position).toString());
    		        	    	Bundle b = new Bundle();
    		        	    	b.putParcelable("RestaurantParcelable",cp);
    		        	    	Message msg = Message.obtain(null, DateBaseService.MSG_DELETE_RESTAURANT, 0, 0);
    		    				msg.setData(b);
    		    				try {
    		    					MainActivity.mService.send(msg);
    		    				} catch (RemoteException e) {
    		    					e.printStackTrace();
    		    				}
    		    				Intent a = new Intent(view.getContext(), Project_Menu.class);
    		    				view.getContext().startActivity(a);
    		         	        }  
    		         	});
    				break;
    			default:
    				super.handleMessage(msg);
    		}
    	}
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_delete__restaurant__db, menu);
		return true;
	}

}
